/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.editor;

import easybooking.data.Airline;
import easybooking.data.Booking;
import easybooking.data.Customer;
import easybooking.data.Flight;
import easybooking.data.Plane;
import easybooking.data.Seat;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 * The table model for bookings
 * @author Tim Schröder
 */
public class BookingTableModel extends AbstractTableModel {

    /**
     * Vector which contains all bookings
     */
    private Vector<Booking> bookings;
    private EBInterfaceClient ebic;
    private Flight f;
    private Plane p;
    private Airline airl;
    private Customer c;
    private Seat s;

    /**
     * The index of the column where the ID get insert
     */
    private static final int ID_INDEX = 0;
    /**
     * The index of the column where the Date get insert
     */
    private static final int DATE_INDEX = 1;
    /**
     * The index of the column where the Flight get insert
     */
    private static final int FLIGHT_INDEX = 2;
    /**
     * The index of the column where the Customer get insert
     */
    private static final int CUSTOMER_INDEX = 3;
    /**
     * The index of the column where the Seat get insert
     */
    private static final int SEAT_INDEX = 4;
    /**
     * The index of the column where the check in Date get insert
     */
    private static final int CHECKINDATE_INDEX = 5;
    /**
     * Index to get the Object which is selected
     */
    private static final int OBJECT = 7;
    /**
     * Names of columns
     */
    private String[] columnNames = {"ID", "Date", "Flight", "Customer", "Seat", "Check in Date"};


    public BookingTableModel(Vector<Booking> b, EBInterfaceClient ebifc) {
        this.ebic = ebifc;
        bookings = b;
    }

    /**
     * get the number of rows the table needs
     * @return the number of booking objects in the bookings Vector
     */
    public int getRowCount() {
        return bookings.size();
    }

    /**
     * get the number of columns
     * @return the number of the column Names
     */
    public int getColumnCount() {
        return columnNames.length;

    }

    @Override
    /**
     * get the name of a column
     * @param col is the number of the column which name is wanted
     * @return the name of a column
     */
    public String getColumnName(int col) {
        return columnNames[col];
    }

    /**
     * prepares the airline data for the table
     * @param rowIndex the row in which the data get insert
     * @param columnIndex the column in which the data get insert
     * @return the prepared object
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        try {
            Booking b = (Booking) bookings.get(rowIndex);
            switch (columnIndex) {
                case ID_INDEX:
                    return b.getBookingId();
                case DATE_INDEX:
                    return b.getDateTime();
                case FLIGHT_INDEX:
                    f = ebic.findFlightByKey(b.getFlightId());
                    p = ebic.findPlaneByKey(f.getPlaneId());
                    airl = ebic.findAirlineByKey(p.getPlaneId());
                    return airl.getInitials() + f.getFlightId();
                case CUSTOMER_INDEX:
                    c = ebic.findCustomerByKey(b.getCustomerId());
                    return c.getFirstname() + " " + c.getLastname();
                case SEAT_INDEX:
                    s = ebic.findSeatByKey(b.getSeatId());
                    return String.valueOf(s.getRow()) + String.valueOf(s.getSeat());
                case CHECKINDATE_INDEX:
                    if (b.getIsCheckedIn()) {
                        return b.getCheckedInDateTime();
                    } else {
                        return "-";
                    }
                case OBJECT:
                    return b;
                default:
                    return new Object();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(BookingTableModel.class.getName()).log(Level.SEVERE, null, ex);
            return new Object();
        }
    }
}

